package com.johnny.dataStructures.sort;

import sun.plugin.javascript.navig.Array;

import java.util.Arrays;

public class ShellSort {
    public static void main(String[] args) {
       //int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 800000);
        }
        //System.out.println(Arrays.toString(arr));
        long begin = System.currentTimeMillis();
        sellSort2(arr);
        long end = System.currentTimeMillis();
        long time = (end - begin);
        //  System.out.println(Arrays.toString(arr));
        System.out.println(time);
    }

    //交换法
    public static void sellSort(int[] arr) {
        int temp = 0;
        int count = 0;
        //循环
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                //遍历各组中所有的元素（共gap组，每组有2个元素，步长gap）
                for (int j = i - gap; j >= 0; j -= gap) {
                    //如果当前元素大于加上步长后的那个元素，说明交换
                    if (arr[j] > arr[j + gap]) {
                        temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
            // System.out.println("第"+(++count)+"轮排序" + Arrays.toString(arr));
        }

    }

    //优化,移位法
    public static void sellSort2(int[] arr) {
        int temp = 0;
        int count = 0;
        //增量gap,并逐步的缩小增量
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            //从第gap个元素，逐个对其所在的组进行直接插入排序
            for (int i = gap; i < arr.length; i++) {
                int j = i;
                temp = arr[j];
                if (arr[j] < arr[j - gap]) {
                    while (j - gap >= 0 && temp < arr[j - gap]) {
                        //移动
                        arr[j] = arr[j - gap];
                        j -= gap;
                    }
                    //当推出while后，就给temp找到插入的位置
                    arr[j]=temp;
                }
            }
            // System.out.println("第"+(++count)+"轮排序" + Arrays.toString(arr));
        }
    }
}
